package com.qdlc.p2p.biz.service.impl;

import com.qdlc.p2p.biz.service.AwardSearchService;
import com.qdlc.p2p.biz.service.CashAwardRuleService;
import com.qdlc.p2p.biz.service.NewActivityPlanService;
import com.qdlc.p2p.biz.service.NewActivityRuleService;
import com.qdlc.p2p.biz.service.activity.*;
import com.qdlc.p2p.biz.service.activity.param.ActivityProgrammeCashAwardParam;
import com.qdlc.p2p.biz.service.activity.param.ActivityProgrammeUserCardParam;
import com.qdlc.p2p.common.enums.EnumCardType;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.MailUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.CashAwardRuleDao;
import com.qdlc.p2p.dal.dao.OperatorDao;
import com.qdlc.p2p.dal.dto.*;
import com.qdlc.p2p.dal.model.AwardModel;
import com.qdlc.p2p.dal.model.CashAwardRuleModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 现金奖励规则Service实现类
 *
 * @author Kaiju Zhao
 * @date 2016-3-23
 */
@Service("cashAwardRuleService")
public class CashAwardRuleServiceImpl implements CashAwardRuleService {

	@Resource
	private CashAwardRuleDao cashAwardRuleDao;
	@Resource
	private OperatorDao operatorDao;
	@Resource
	private AwardSearchService awardSearchService;
	@Resource
	private NewActivityPlanService newActivityPlanService;
	@Resource
	private NewActivityRuleService newActivityRuleService;

	@Override
	public int saveCashAwardRule(CashAwardRuleModel model) {
		CashAwardRule cashAwardRule = CashAwardRuleModel.prototype(model);
		cashAwardRule.setCreator(model.getCreator());
		cashAwardRule.setCreateTime(model.getCreateTime());
		return cashAwardRuleDao.save(cashAwardRule);
	}

	@Override
	public int updateCashAwardRule(CashAwardRuleModel model) {
		long id = model.getId();
		CashAwardRule oldCashAwardRule = cashAwardRuleDao.find(id);
		if (null == oldCashAwardRule) {
			return 0;
		}
		CashAwardRule cashAwardRule = CashAwardRuleModel.prototype(model);
		cashAwardRule.setId(id);
		cashAwardRule.setModifier(model.getModifier());
		cashAwardRule.setModifyTime(model.getModifyTime());
		cashAwardRule.setCreator(oldCashAwardRule.getCreator());
		cashAwardRule.setCreateTime(oldCashAwardRule.getCreateTime());
		return cashAwardRuleDao.update(cashAwardRule);
	}

	@Override
	public int toggleStatus(CashAwardRuleModel model) {
		return cashAwardRuleDao.toggleStatus(model);
	}

	@Override
	public CashAwardRuleModel findCashAwardRuleById(long id) {
		CashAwardRuleModel cashAwardRuleModel = null;
		CashAwardRule cashAwardRule = cashAwardRuleDao.find(id);
		if (null != cashAwardRule) {
			cashAwardRuleModel = CashAwardRuleModel.instance(cashAwardRule);
			if (cashAwardRule.getCreator()!=0) {
				Operator operator = operatorDao.userFind(cashAwardRule.getCreator());
				cashAwardRuleModel.setCreatorName(operator.getUserName());
			}
			if (cashAwardRule.getModifier()!=0) {
				Operator operator = operatorDao.userFind(cashAwardRule.getModifier());
				cashAwardRuleModel.setModifierName(operator.getUserName());
			}
		}
		return cashAwardRuleModel;
	}

	@Override
	public PageDataList<CashAwardRuleModel> list(CashAwardRuleModel model) {
		return cashAwardRuleDao.list(model);
	}

	@Override
	public void giveMoney(User user, double money) {
		//定义活动计划
		try {
			ActivityPlanParam planParam = new ActivityPlanParam();
			planParam.setActivityPlan(ActivityPlanEnum.FIRST_TENDER);
			planParam.setUser(user);
			ActivityProgrammeCashAwardParam programmeParam = new ActivityProgrammeCashAwardParam();
			programmeParam.setAmount(money);
			planParam.addProgrammeParam(ActivityProgrammeEnum.CASH_AWARD, programmeParam);
			ActivityPlanHandlers.defaultHandle(planParam);
		} catch (ActivityPlanException e) {
			e.printStackTrace();
			MailUtil.sendMail("给用户" + user.getUserName() + "发送现金：" + money + "失败,需要补发.");
		}
	}

	@Override
	public void giveAwards(long borrowId) {
		try {
			//定义活动计划
	    	AwardModel firstTender = awardSearchService.firstTender(borrowId);
		    AwardModel lastTender = awardSearchService.lastTender(borrowId);
		    AwardModel mostTender = awardSearchService.mostTender(borrowId);

			//先声夺人颁奖
			User firstUser  = new User();
			firstUser.setUserId(firstTender.getUserId());
			ActivityPlanParam firstPlanParam = new ActivityPlanParam();
			firstPlanParam.setActivityPlan(ActivityPlanEnum.SYNTHETIC_APERTURE);
			firstPlanParam.setUser(firstUser);
			ActivityProgrammeCashAwardParam firstProgrammeParam = new ActivityProgrammeCashAwardParam();
			firstProgrammeParam.setAmount(firstTender.getMoney());
			firstPlanParam.addProgrammeParam(ActivityProgrammeEnum.CASH_AWARD, firstProgrammeParam);


			if(!(firstTender.getUserId()==lastTender.getUserId()&&firstTender.getUserId()==mostTender.getUserId())) {
				//大功告成颁奖
				User lastUser = new User();
				lastUser.setUserId(lastTender.getUserId());
				ActivityPlanParam lastPlanParam = new ActivityPlanParam();
				lastPlanParam.setActivityPlan(ActivityPlanEnum.BE_ACCOMPLISHED);
				lastPlanParam.setUser(lastUser);
				ActivityProgrammeCashAwardParam lastProgrammeParam = new ActivityProgrammeCashAwardParam();
				lastProgrammeParam.setAmount(lastTender.getMoney());
				lastPlanParam.addProgrammeParam(ActivityProgrammeEnum.CASH_AWARD, lastProgrammeParam);
				ActivityPlanHandlers.defaultHandle(lastPlanParam);
			}

			User mostUser = new User();
			mostUser.setUserId(mostTender.getUserId());
			ActivityPlanParam mostPlanParam = new ActivityPlanParam();
			mostPlanParam.setActivityPlan(ActivityPlanEnum.GAMBLE_AT_HIGH_STAKES);
			mostPlanParam.setUser(mostUser);
			ActivityProgrammeCashAwardParam mostProgrammeParam = new ActivityProgrammeCashAwardParam();
			mostProgrammeParam.setAmount(mostTender.getMoney());
			mostPlanParam.addProgrammeParam(ActivityProgrammeEnum.CASH_AWARD,mostProgrammeParam);

			ActivityPlanHandlers.defaultHandle(firstPlanParam);
			ActivityPlanHandlers.defaultHandle(mostPlanParam);

		} catch (ActivityPlanException e) {
			e.printStackTrace();
			MailUtil.sendMail("后台颁奖出错,,需要补发.");
		}
	}

	@Override
	public void getCashAwardRule(Map resMap, String enTache, String remark,long startTime) {
		//检测活动是否可行
		//获取活动计划环节
		NewActivityPlan activityPlan = newActivityPlanService.getActivityPlanByTache(enTache);
		if(activityPlan ==null){
			return;
		}
		if(activityPlan.getStatus()!=0||(activityPlan.getStartTime().getTime()> startTime|| activityPlan.getEndTime().getTime() < startTime)){
			return ;
		}
		//查找该活动计划环节中活动方案
		List<NewActivityRule> activityProgrammeList = newActivityRuleService.findByActivityId(activityPlan.getId());
		for(NewActivityRule activityProgramme: activityProgrammeList){
			//活动方案类型
			int type = activityProgramme.getType();
            if(type!=ActivityProgrammeEnum.CASH_AWARD.getType()){
				continue;
			}else{
			 CashAwardRule cashAwardRule = cashAwardRuleDao.find(activityProgramme.getRuleId());
			 if(cashAwardRule.getExpiredTime().getTime() < startTime){
				 break;
			  }
			 else{
				 resMap.put(remark+"CashBackRate",cashAwardRule.getCashBackRate());
			 }
			}
		}
		return ;
	}

	@Override
	public void giveRebate(User user, double money) {
		//定义活动计划
		try {
			ActivityPlanParam planParam = new ActivityPlanParam();
			planParam.setActivityPlan(ActivityPlanEnum.AUTOMATIC_REBATE);
			planParam.setUser(user);
			ActivityProgrammeCashAwardParam programmeParam = new ActivityProgrammeCashAwardParam();
			programmeParam.setAmount(money);
			planParam.addProgrammeParam(ActivityProgrammeEnum.CASH_AWARD, programmeParam);
			ActivityPlanHandlers.defaultHandle(planParam);
		} catch (ActivityPlanException e) {
			e.printStackTrace();
			MailUtil.sendMail("给用户" + user.getUserName() + "发送现金：" + money + "失败,需要补发.");
		}
	}

}
